package com.jiu.bean;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CmdBean {
	/**
	 * @param msg
	 * @return
	 */
	public static String getNewCmd(String msg) {
		String cmd = msg.charAt(msg.length()-1)+"";
		cmd = "#D"+cmd.toLowerCase();
		return cmd;
	}
	/**
	 * @param group
	 * @return
	 */
	String[] question = {
            " 6-5 =（z÷1） ","8+（z÷1）=10","2z+4=6","（8+z）+9=20",
            "9-（z-4）=6","9+（5+z）=16",
            "2×（z-7）=4","7÷（5+z）=1","7-（z÷2）=4","3×（1+z）=21","6+（1+z）=14",
            "（7+z）+8=18","14-4=（7+z）","20÷（z÷2）=5","13-（2+z）=3","9×（z÷4）=18","8÷2=2z","4x=8+4",
            "20-17z=3"
            
           };
	//售鱼命令：#D zz，z为：D 
	//售鱼命令：#D zz，z为：（10+z）+3=1
	public static String getSellCmd(String msg, String fishCmd) {
		String math = "为：([\\s\\S]*[+-×÷][\\s\\S]*)";
		String cmd = "";		
		try {
			Pattern	pattern = Pattern.compile(math);
			Matcher	matcher = pattern.matcher(msg);		
			if(msg.contains("+")||msg.contains("-")||msg.contains("×")||msg.contains("÷")){
				if(matcher.find()){	
					System.out.print(matcher.group()+"  ");
					Integer result = getQuestionResult(matcher.group(1));
					String r = "";
					if(result<0||result>10){
						r = result.toString().charAt(1)+"";
					}else{
						r = result.toString();
					}
					cmd = fishCmd.substring(0,3)+" "+r+r;
				}			
			}else{
				char last = msg.charAt(msg.length()-1);
				
				cmd = ""+last+last;
				cmd = cmd.toLowerCase();
				cmd = fishCmd.substring(0,3)+" "+cmd;
				
			}
		} catch (Exception e) {			
		}
		return cmd;
	}
    private static Integer getQuestionResult(String question) {
    	question = question.replaceAll("[？*]", "z");
	String[] eq = question.trim().split("=");
	Integer ans = 0;
	for(int i=0;i<2;i++){
		Pattern pattern = Pattern.compile("[+×÷-]||\\d\\w");
		Matcher matcher = pattern.matcher(eq[i]);
		if(matcher.find()){
			pattern = Pattern.compile("(\\d{1,2})([a-zA-Z])");
			matcher = pattern.matcher(eq[i]);
			if(matcher.find()){
				eq[i] = eq[i].replace(matcher.group(), "（"+matcher.group(1)+"×"+matcher.group(2)+"）");
			}
			if(eq[i].contains("（")){
				pattern = Pattern.compile("（\\w{1,2}[+×÷-]\\w{1,2}）");
				matcher = pattern.matcher(eq[i]);
				if(matcher.find()){
					int other = i==0?1:0;
					Integer rrnum = 0;
					if(eq[other].length()>2){
						Pattern pattern1 = Pattern.compile("(\\d{1,2})([+-×÷]{1})(\\d{1,2})");
						Matcher matcher1 = pattern1.matcher(eq[other]);
						if(matcher1.find()){							
							rrnum = getResult(matcher1.group(1),matcher1.group(2),matcher1.group(3));
						}
					}else{
						String align="left",sign="",num="",rnum,temp = matcher.group();
						eq[i] = eq[i].replace(temp, "");
						if(eq[i].charAt(0)>=48&&eq[i].charAt(0)<=57)
							align = "right";
						rnum =eq[other];
						for(int j=0;j<eq[i].length();j++){
							if(eq[i].charAt(j)>=48&&eq[i].charAt(j)<=57){
								num += eq[i].charAt(j);
							}else{
								sign += eq[i].charAt(j);
							}						
						}					
						rrnum = execute(num,sign,rnum,align);
						
					}
					String align="left",sign="",num="",temp = matcher.group();
					if(temp.charAt(1)>=48&&temp.charAt(1)<=57){
						align = "right";						
					}
					for(int j=0;j<temp.length();j++){
						if(temp.charAt(j)>=48&&temp.charAt(j)<=57){
							num += temp.charAt(j);
						}else{
							pattern = Pattern.compile("[+×÷-]");
							matcher = pattern.matcher(temp);
							if(matcher.find())
								sign = matcher.group();
						}						
					}
					ans = execute(num,sign,rrnum.toString(),align);
				}
			}
		}
	}
	return ans;
    }

	private static Integer execute(String num, String sign,
			String rnum, String align) {
		Integer temp = 0;
		if(sign.equals("+")){
			temp = Integer.parseInt(rnum)-Integer.parseInt(num);
		}else if(sign.equals("-")){
			if(align.equals("left")){
				temp = Integer.parseInt(rnum)+Integer.parseInt(num);
			}else{
				temp =Integer.parseInt(num)- Integer.parseInt(rnum);
			}
		}else if(sign.equals("×")){
			temp = Integer.parseInt(rnum)/Integer.parseInt(num);
		}else if(sign.equals("÷")){
			if(align.equals("left")){
				temp = Integer.parseInt(rnum)*Integer.parseInt(num);
			}else{
				temp = Integer.parseInt(num)/Integer.parseInt(rnum);
			}
		}
		return temp;
	}
	private static Integer getResult(String str1, String sign, String str2) {
		int num1 = Integer.parseInt(str1);
		int num2 = Integer.parseInt(str2);
		if(sign.equals("+")){
			Integer r = num1 + num2;
			return r;
		}else if(sign.equals("-")){
			Integer r = num1 - num2;
			return r;
		}else if(sign.equals("×")){
			Integer r = num1 * num2;
			return r;
		}else if(sign.equals("÷")){
			Integer r = num1 / num2;
			return r;
		}
		return 0;
	}
	@SuppressWarnings("unused")
	private String getResult2(String str1, String sign, String str2) {
		int num1 = Integer.parseInt(str1);
		int num2 = Integer.parseInt(str2);
		if(sign.equals("+")){
			Integer r = num1 + num2;
			return r.toString()+r.toString()+r.toString();
		}else if(sign.equals("-")){
			Integer r = num1 - num2;
			return r.toString()+r.toString()+r.toString();
		}else if(sign.equals("×")){
			Integer r = num1 * num2;
			return r.toString()+r.toString()+r.toString();
		}else if(sign.equals("÷")){
			Integer r = num1 / num2;
			return r.toString()+r.toString()+r.toString();
		}else{
			return "me";
		}
	}
	
	public static void main(String[] args) {
		System.out.println(new FishThread().isException("s sdfsdf-234次）fsdfsdf"));
		System.out.println(getNewCmd("☆下次钓鱼请用：#dc"));//#D zz，z为：3+13z=16 
		System.out.println(getSellCmd("售鱼命令：#D XX，X为：17-6=11X","#dd"));
		System.out.println(getSellCmd("售鱼命令：#D XX，X为：2×6X=12","#dd"));
		System.out.println(getSellCmd("售鱼命令：#D XX，X为：5X+2=7","#dd"));
		System.out.println(getSellCmd("售鱼命令：#D XX，X为：（X÷1）+9=10","#dd"));
		

		System.out.println(getSellCmd("12〖水〗：售鱼命令：#D XX，X为：2×7X=14","#dd"));
		System.out.println(getSellCmd("12〖水〗：售鱼命令：#D XX，X为：3×2X=6","#dd"));
		System.out.println(getSellCmd("12〖水〗：售鱼命令：#D XX，*为：11-2*=9","#dd"));
		System.out.println(getSellCmd("6〖语嫣〗：售鱼命令：#D zz，z为：（7+z）+4=14","#dd"));
		
				
				
				
				
	}
	
}
